<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/24 16:46
 * Remark:
 */

namespace App\Http\Middleware\Position;

use App\Consts\AuthConsts\AuthConsts;
use App\Consts\BaseConsts\BaseConsts;
use App\Consts\RequestConsts\RequestMethodConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Auth\AuthBasicModel;
use App\Models\Auth\AuthMenuModel;
use App\Models\Department\DepartmentModel;
use App\Models\Position\PositionModel;
use Closure;
use Illuminate\Http\Exceptions\HttpResponseException;

class PositionLegitimacyMiddleware
{
    private PositionModel $_positionModel;
    private DepartmentModel $_departmentModel;

    public function __construct()
    {
        $this->_positionModel = new PositionModel();
        $this->_departmentModel = new DepartmentModel();
    }

    public function handle($request, Closure $next)
    {
        $method = strtolower($request->method());
        switch ($method){
            case RequestMethodConsts::GET_METHOD:
                //
                break;
            case RequestMethodConsts::POST_METHOD:
                // 检验同部门下是否有相同名称
                $requestData = $request->post();
                $this->verifyPositionNameIsExists($requestData['position_name'], $requestData['department_id']);
                // 新增校验部门是否属于当前公司或存在
                $companyId = TokenFuc::getCurrentStaffInfo()['company_id'] ?? 0;
                $this->verifyDepartmentAndCompanyIdIsExists($requestData['department_id'], $companyId);
                break;
            case RequestMethodConsts::PUT_METHOD:
                $requestData = $request->post();
                $positionId = request()->positionId;
                // 编辑部门时查看当前部门下是否有相同名称
                if(isset($requestData['position_name']) && $requestData['position_name']){
                    $this->verifyPositionNameByPositionIdIsExists($positionId, $requestData['position_name']);
                }
                if(isset($requestData['department_id']) && $requestData['department_id']){
                    throw new HttpResponseException( ResponseFunc::returnJson(
                        'error',
                        null,
                        "不允许修改部门",
                        ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                    ));
                }
                break;
            case RequestMethodConsts::PATCH_METHOD:

                $positionId = $request->positionId;
                $positionIds = $request->post('position_ids') ?? null;
                if($positionIds){
                    foreach($positionIds as $positionId){
                        // 删除的时候校验职位是否存在
                        $this->verifyPositionIsExists($positionId,$positionId."职位不存在或已删除");
                    }
                }

                if($positionId){
                    $this->verifyPositionIsExists($positionId,"职位不存在或已删除");
                }
                break;
            default:
                break;
        }

        return $next($request);
    }

    /**
     *  校验职位名称是否存在
     * @param string $positionName
     * @param int $departmentId
     */
    private function verifyPositionNameIsExists(string $positionName, int $departmentId){
        $isExist = $this->_positionModel->isExistByCondition([
            'position_name' => $positionName,
            'department_id' => $departmentId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前部门已存在同名职位",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }

    /**
     * 校验修改名称是否存在
     * @param int $positionId
     * @param string $positionName
     */
    private function verifyPositionNameByPositionIdIsExists(int $positionId, string $positionName){
        $positionInfo = $this->_positionModel->getPositionInfoByPositionId($positionId,['department_id']);
        $isExist = $this->_positionModel->legPositionByEdit($positionId,$positionName,$positionInfo['department_id']);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前部门已存在同名职位",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }

    /**
     * 检查当前公司下部门是否存在
     * @param int $departmentId
     * @param int $companyId
     */
    private function verifyDepartmentAndCompanyIdIsExists(int $departmentId, int $companyId){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_id' => $departmentId,
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前部门不存在或已删除",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }

    /**
     * 校验职位是否存在
     * @param $positionId
     * @param $msg
     */
    private function verifyPositionIsExists($positionId,$msg){
        $isExist = $this->_positionModel->isExistByCondition([
            'position_id' => $positionId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }


}